package  org.fantasy.common.eval;

import java.math.BigDecimal;

import bsh.Interpreter;

public class BSHCalculator implements Calculator {
    private Interpreter interpreter;
    private String expression;
    private String result;

    public BSHCalculator() {
	interpreter = new Interpreter();
    }

    public BSHCalculator(Interpreter interpreter) {
	this.interpreter = interpreter;
    }

    public BSHCalculator(Interpreter interpreter, String exp) throws Exception {
	this(interpreter);
	input(exp);
    }

    public BSHCalculator(String exp) throws Exception {
	this();
	input(exp);
    }

    public void input(String exp) throws Exception {
	this.expression = exp;
	this.result = interpreter.eval(expression).toString();
    }

    public BigDecimal eval() throws Exception {
	if (expression == null)
	    return new BigDecimal(0);
	else
	    return new BigDecimal(result);
    }

    public static void main(String[] args) {

    }
}
